package com.luisliuyi.demo.hook.ams;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class AmsPHooker extends AmsHooker{
    @Override
    protected Object getHookObj() {
        Class amClass = ReflectUtils.getClass("android.app.ActivityManager");
        // 拿到 IActivityManagerSingleton 属性
        return ReflectUtils.readStaticField(amClass, "IActivityManagerSingleton");

    }

    @Override
    protected Field getHookField() {
        // 获取 mInstance Field
        return ReflectUtils.getField(ReflectUtils.getClass("android.util.Singleton"), "mInstance") ;
    }

    @Override
    Object getTarget() {
        // ActivityManager.getService()返回 am
        Class amClass = ReflectUtils.getClass("android.app.ActivityManager");
        if (amClass != null) {
            try {
                Method method = amClass.getDeclaredMethod("getService");
                if (method != null) {
                    return method.invoke(null);
                }
            } catch (Exception e) {

            }
        }
        return null;
    }

    @Override
    Object getInterfaces() {
        // 获取interfaces，用来创建动态代理
        Class IActivityManagerClz = ReflectUtils.getClass("android.app.IActivityManager");
        return IActivityManagerClz;
    }
}
